package com.zxj.userscenter.users.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zxj.userscenter.common.util.ConstatFinalUtil;
import com.zxj.userscenter.common.util.PageInfoUtil;
import com.zxj.userscenter.users.dao.IAAdminsDao;
import com.zxj.userscenter.users.dao.IARoleDao;
import com.zxj.userscenter.users.dao.IAUsersDao;
import com.zxj.userscenter.users.dao.IAUsersExtendDao;
import com.zxj.userscenter.users.pojo.AAdmins;
import com.zxj.userscenter.users.pojo.ARole;
import com.zxj.userscenter.users.pojo.AUsers;
import com.zxj.userscenter.users.pojo.AUsersExtend;
import com.zxj.userscenter.users.service.IUsersService;
/**
 * 用户相关的service实现类
 * @author ASUS
 *
 */
@Service("usersService")
public class UsersServiceimpl implements IUsersService
{
	@Resource
    private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	@Resource
	private IAUsersDao usersDao;
	@Resource
	private IAUsersExtendDao usersExtendDao;
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		/*影响条数*/
		int res=this.adminsDao.save(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info",  ConstatFinalUtil.INFO_JSON.get("0"));
			/*存储date*/
			JSONObject dateObject=new JSONObject();
			dateObject.put("id", admins.getId()+"");
			dateObject.put("effect", res+"");
			resultJSON.put("date", dateObject);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		/*影响条数*/
		int res=this.adminsDao.update(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			/*存储date*/
			JSONObject dateObject=new JSONObject();
			dateObject.put("id", admins.getId()+"");
			dateObject.put("effect", res+"");
			resultJSON.put("date", dateObject);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info",  ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		
		JSONObject resultJSON=new JSONObject();
		/*影响条数*/
		int res=this.adminsDao.delete(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			/*存储date*/
			JSONObject dateObject=new JSONObject();
			dateObject.put("id", admins.getId()+"");
			dateObject.put("effect", res+"");
			resultJSON.put("date", dateObject);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info",  ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condmap)
	{
		return this.adminsDao.findOne(condmap);
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/*为关键字加上%，实现模糊查询*/
		if(condMap.get("keyword")!=null &&!"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null)
		{
			/*分页*/
			/*查询总记录数*/
			condMap.put("pageCount", "true");
			List<AAdmins> roleList=this.adminsDao.findCondList(condMap);
			if(roleList.size()==1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			/*查询数据*/
			condMap.put("pageCount", "false");
			/*limit?,?*/
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminsDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.adminsDao.findCondList(condMap);
	}
    @Override
	public JSONObject saveOneRoleService(ARole role)
	{
		JSONObject resultJSON=new JSONObject();
		/*影响条数*/
		int res=this.roleDao.save(role);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info",  ConstatFinalUtil.INFO_JSON.get("0"));
			/*存储date*/
			JSONObject dateObject=new JSONObject();
			dateObject.put("id", role.getId()+"");
			dateObject.put("effect", res+"");
			resultJSON.put("date", dateObject);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role)
	{
		JSONObject resultJSON=new JSONObject();
		/*影响条数*/
		int res=this.roleDao.update(role);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			/*存储date*/
			JSONObject dateObject=new JSONObject();
			dateObject.put("id", role.getId()+"");
			dateObject.put("effect", res+"");
			resultJSON.put("date", dateObject);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info",  ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneRoleService(ARole role)
	{
		
		JSONObject resultJSON=new JSONObject();
		/*影响条数*/
		int res=this.roleDao.delete(role);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			/*存储date*/
			JSONObject dateObject=new JSONObject();
			dateObject.put("id", role.getId()+"");
			dateObject.put("effect", res+"");
			resultJSON.put("date", dateObject);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info",  ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public ARole findOneRoleService(Map<String, Object> condmap)
	{
		return this.roleDao.findOne(condmap);
	}

	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/*为关键字加上%，实现模糊查询*/
		if(condMap.get("keyword")!=null &&!"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null)
		{
			/*分页*/
			/*查询总记录数*/
			condMap.put("pageCount", "true");
			List<ARole> roleList=this.roleDao.findCondList(condMap);
			if(roleList.size()==1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			/*查询数据*/
			condMap.put("pageCount", "false");
			/*limit?,?*/
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.roleDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.roleDao.findCondList(condMap);
	}
	@Override
	public AUsers findOneUsersService(Map<String, Object> condMap)
	{
		return this.usersDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 检查邮箱不能重复 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("email", users.getEmail());
		AUsers usersRes = this.findOneUsersService(condMap);
		if(usersRes != null)
		{
			/* 邮箱已经存在,请更换 */
			resultJSON.put("code", "12");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("12"));
			return resultJSON ;
		}
		
		/* 影响条数 */
		int res = this.usersDao.save(users);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersDao.update(users);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersDao.delete(users);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public List<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condMap.put("pageCount", "true");
			List<AUsers> roleList = this.usersDao.findCondList(condMap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.usersDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.usersDao.findCondList(condMap);
	}
	
	@Override
	public AUsersExtend findOneUsersExtendService(Map<String, Object> condMap)
	{
		return this.usersExtendDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneUsersExtendService(AUsersExtend usersExtend)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 影响条数 */
		int res = this.usersExtendDao.save(usersExtend);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersExtend.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersExtendService(AUsersExtend usersExtend)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersExtendDao.update(usersExtend);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersExtend.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneUsersExtendService(AUsersExtend usersExtend)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersExtendDao.delete(usersExtend);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersExtend.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public List<AUsersExtend> findCondListUsersExtendService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condMap.put("pageCount", "true");
			List<AUsersExtend> roleList = this.usersExtendDao.findCondList(condMap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.usersExtendDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.usersExtendDao.findCondList(condMap);
	}
}
